package com.example.demo.controller;


import com.example.demo.entity.Apply;
import com.example.demo.entity.User;
import com.example.demo.entity.dto.ResultBean;
import com.example.demo.exception.bizException.BizException;
import com.example.demo.exception.bizException.BizExceptionCodeEnum;
import com.example.demo.service.ApplyService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *报名情况
 * @author cy
 * @since 2020-08-01
 * @version v1.0
 */
@RestController
@RequestMapping("/apply")
public class ApplyController {

    @Autowired
    private ApplyService applyService;

    /**
    * 查询分页数据
    */
    @RequestMapping(method = RequestMethod.GET)
    public ResultBean<?> listByPage(@RequestParam(name = "page", defaultValue = "1") int page,
                                    @RequestParam(name = "pageSize", defaultValue = "10") int pageSize,
                                    @RequestParam(name = "dId", defaultValue = "") Integer dId ,
                                    @RequestParam(name = "uId", defaultValue = "") Integer uId ) {
        return new ResultBean<>(applyService.listApplysByPage(page, pageSize,dId,uId));
    }

    @RequestMapping(method = RequestMethod.GET,value = "/normal/{id}")
    public ResultBean<?> listApplysById(@PathVariable("id") Integer id) {
        return new ResultBean<>(applyService.listApplysById(id));
    }


    /**
    * 根据id查询
    */
    @RequestMapping(method = RequestMethod.GET, value = "/{id}")
    public ResultBean<?> getById(@PathVariable("id") Integer id) {
        return new ResultBean<>(applyService.getApplyById(id));
    }

    /**
    * 新增
    */
    @RequestMapping(method = RequestMethod.POST)
    public ResultBean<?> insert(@RequestBody Apply apply) {
        User user= (User) SecurityUtils.getSubject().getPrincipal();
        if(user==null){
            throw new BizException(BizExceptionCodeEnum.NO_LOGIN);
        }
        apply.setUId(user.getUserId());
        return new ResultBean<>(applyService.insertApply(apply));
    }

    /**
    * 删除
    */
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
    public ResultBean<?> deleteById(@PathVariable("id") Integer id) {
        User user= (User) SecurityUtils.getSubject().getPrincipal();
        if(user==null){
            throw new BizException(BizExceptionCodeEnum.NO_LOGIN);
        }
        return new ResultBean<>(applyService.deleteApplyById(id,user.getUserId()));
    }

    /**
    * 修改
    */
    @RequestMapping(method = RequestMethod.PUT)
    public ResultBean<?> updateById(@RequestBody Apply apply) {
        return new ResultBean<>(applyService.updateApply(apply));
    }
}
